package com.xfz.Controller;


import com.xfz.Mapper.UserMapper;
import com.xfz.User.Supplier;
import com.xfz.User.User;
import com.xfz.User.UserLogin;
import com.xfz.User.UserReqister;
import com.xfz.common.Res;
import com.xfz.service.SupplierService;
import com.xfz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;



//注册
    @PostMapping("/register")
    public Res register(@RequestBody @Validated(UserReqister.class) User user) {
        if (userService.register(user) != null) {
            return new Res(1, "注册成功", user);
        } else {
            return new Res(0, "注册失败");
        }
    }

    //登录
    @PostMapping("/login")
    public Res login(@RequestBody @Validated(UserLogin.class) User user) {
        User loginUser = userService.login(user);
        if (loginUser != null) {
            return new Res(1, "登录成功", loginUser);
        } else {
            return new Res(0, "登录失败");
        }
    }

    //保存token
    @GetMapping("/mine/info")
    public Res getMyInfo(@RequestHeader("TOKEN") String token) {
        User currentUser = userService.getUserByToken(token);
        currentUser.setPassword(null);
        return new Res(1, "成功", currentUser);
    }


//修改密码
    @PostMapping("/updatepwd")
    public Res updatepwd(@RequestBody User user) {
        if (userService.updatepass(user)) {
            return new Res(1, "修改成功", user);
        } else {
            return new Res(0, "修改失败");
        }
    }

    //用户查询
    @GetMapping("/Jurisdiction")
    public Res Jurisdiction() {
        List<User>  users = userService.userNames();
        if(users!=null){
            return new Res(1, "成功", users);
        } else {
            return new Res(0, "失败");
        }

    }


//    删除用户
    @GetMapping("/delete/{id}")
    public Res deluser(@PathVariable("id") Integer id) {
       Integer users = userMapper.deluser(id);
        if(users>0){
            return new Res(1, "删除成功", users);
        } else {
            return new Res(0, "删除失败");
        }

    }
}